<?php

autoLoader();

if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
	fetchById();
	exit;
	
}

if( true == isset( $_GET['patient_name'] ) && true == valStr( $_GET['patient_name'] ) ) {
	fetchByName();
	exit;
}

if( true == isset( $_GET['patient_data'] ) && true == valStr( $_GET['patient_data'] ) ) {
	if( true == is_numeric( $_GET['patient_data'] ) ) {
		$objPatient = fetchById( $_GET['patient_data'] );
	} else {
		$objPatient = fetchByName( $_GET['patient_data'] );
	}
	
	if( false == valObj( $objPatient, 'BasePatients' ) ) {
		$objPatient = fetchByName();
	}
	
	if( true == valObj( $objPatient, 'BasePatients' ) ) {
		$intCasePaperId = findCasePaperId( $objPatient->getId() );
		
		echo $objPatient->getId() . '_' . $objPatient->getFirstName() . '_' . $objPatient->getLastName() . '_' . $intCasePaperId;
		exit;
	} else {
		echo 'not found::';
		exit;
	}
	
	exit;
}

function fetchById( $intPatientId = NULL ) {
	$objPatient = new Patients();
	
	$intPatientId = ( true == valStr( $intPatientId ) ) ? $intPatientId : $_GET['patient_id'];
	
	$objPatient = $objPatient->fetchPatientsById( $intPatientId );
	
	if( true == isset( $_GET['patient_data'] ) && true == valStr( $_GET['patient_data'] ) ) {
		return $objPatient;
	}
	
	$intCasePaperId = findCasePaperId( $intPatientId );
	
	if( true == valObj( $objPatient, 'BasePatients' ) ) {
		echo $objPatient->getId() . '_' . $objPatient->getFirstName() . '_' . $objPatient->getLastName() . '_' . $intCasePaperId;
		exit;
	} else {
		echo 'not found::';
		exit;
	}
	exit;
}

function fetchByName( $strPatientName = NULL ) {
	$objPatient = new Patients();
	
	$strPatientName = ( true == valStr( $strPatientName ) ) ? $strPatientName : $_GET['patient_name'];
	
	$arrstrName = explode( ' ', $strPatientName );
	
	$strFirstName 	= getArrayElementByKey( 0, $arrstrName );
	$strLastName 	= getArrayElementByKey( 1, $arrstrName );
	
	$strSql = 'SELECT * FROM PATIENTS WHERE first_name = \'' . $strFirstName . '\' AND last_name = \'' . $strLastName . '\'';
	$arrobjPatients = $objPatient->fetchSql( $strSql );
	
	$arrobjAllPatients = array();
	
	if( true == valArr( $arrobjPatients ) ) {
		foreach( $arrobjPatients as $objPatient ) {
			$strName = $objPatient->getFirstName() . ' ' . $objPatient->getLastName();
			$arrobjAllPatients[strtolower( $strName )] = $objPatient;
		}
	}
	
	$strName = $strFirstName . ' ' . $strLastName;
	$objPatient = getArrayElementByKey( strtolower( $strName ), $arrobjAllPatients );
	
	if( true == valObj( $objPatient, 'BasePatients' ) ) {
		$intCasePaperId = findCasePaperId( $objPatient->getId() );
	
		echo $objPatient->getId() . '_' . $objPatient->getFirstName() . '_' . $objPatient->getLastName() . '_' . $intCasePaperId;
		exit;
	} else {
		echo 'not found::';
		exit;
	}
	exit;
}

function findCasePaperId( $intPatientId ) {
	if( false == valStr( $intPatientId ) ) return NULL;
	
	$objConnection = new Patients();
	
	$strSql = 'SELECT max( id ) as id FROM case_papers WHERE patient_id = ' . $intPatientId;
	$result = $objConnection->connection->RunQuery( $strSql );
	
	$intCasePaperId = NULL;
	if( true == $result ) {
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$intCasePaperId = $row['id'];
		}
	}
	
	return $intCasePaperId;
}

echo 'not found::';
exit;
?>